﻿#pragma once
#include "oatpp/web/server/HttpRequestHandler.hpp"
#include "oatpp/web/url/mapping/Pattern.hpp"
#include "../util/FileUtil.h"
#include "../log/SysLog.h"

class WebFileHandler
	: public oatpp::web::server::HttpRequestHandler
{
public:
	/**
	 * 静态文件下载
	 */
	std::shared_ptr<OutgoingResponse> handle(const std::shared_ptr<IncomingRequest>& request) override {
		// given request path="/hello/path/tail" for path-pattern="/hello/\*", tail == "path/tail"
		std::string sub_path = request->getPathTail().getValue("");
		int pos = sub_path.find("?");
		if (pos != -1)
		{
			sub_path = sub_path.substr(0, pos);
		}
		std::string dir = FileUtil::GetAppPath("app/");
		std::string path = dir + sub_path;
		if (!FileUtil::Exists(path) || FileUtil::IsDir(path))
		{
			return ResponseFactory::createResponse(Status::CODE_404, "do not found this file:" + dir);
		}
		std::string content = FileUtil::Read(path);
		return ResponseFactory::createResponse(Status::CODE_200, content);
	}
};